<script setup>
import { ref } from 'vue'
import { useUserStore } from '../stores/userStore'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const router = useRouter()

const loginForm = ref({
  username: '',
  password: ''
})

const handleLogin = () => {
  if (loginForm.value.username && loginForm.value.password) {
    // 模拟登录成功
    userStore.login({
      nickname: loginForm.value.username,
      avatarUrl: '/images/avatars/' + loginForm.value.username + '.jpg'
    })
    
    ElMessage.success('登录成功')
    router.push('/')
  } else {
    ElMessage.error('请输入用户名和密码')
  }
}
</script>


<template>
  <el-row>
    <el-col :md="24" :lg="8"></el-col>
    <el-col :md="24" :lg="8">
      <el-form :model="loginForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input 
            ref="usernameInput"
            v-model="loginForm.username" 
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input 
            ref="passwordInput"
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :md="24" :lg="8"></el-col>
  </el-row>
</template>